﻿using EnterpriseWebsiteApi.ActionFilters;
using EnterpriseWebsiteApi.ActionResults;
using EnterpriseWebsiteApi.Dto;
using EnterpriseWebsiteApi.Dto.Product;
using EnterpriseWebsiteBLL;
using EnterpriseWebsiteUtil.Attributes;
using EnterpriseWebsiteUtil.Helper;
using Microsoft.AspNetCore.Http;
using Microsoft.AspNetCore.Mvc;
using SqlSugar;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;

namespace EnterpriseWebsiteApi.Controllers.Web
{
    [Route("api/web/[controller]/[action]")]
    [ApiController]
    [ServiceFilter(typeof(LanguageHeaderFilter))]
    public class ProductController : ControllerBase
    {
        private readonly ProductService _productService;
        private readonly LangSplitHelper _langSplitHelper;
        private readonly ProductPriceCurrenciesService  _productPriceCurrenciesService;
        private readonly Mapper _mapper;

        public ProductController(ProductService productService, ProductPriceCurrenciesService productPriceCurrenciesService, LangSplitHelper langSplitHelper, Mapper mapper)
        {
            _productService = productService;
            _productPriceCurrenciesService = productPriceCurrenciesService;
            _langSplitHelper = langSplitHelper;
            _mapper = mapper;
        }

        /// <summary>
        /// 获取商品列表
        /// </summary>
        /// <param name="language"></param>
        /// <param name="categorieId"></param>
        /// <param name="pageIndex"></param>
        /// <param name="pageSize"></param>
        /// <returns></returns>
        [HttpGet]
        public async Task<ActionResult> GetProductList(LangType language,int categorieId = 0,int pageIndex = 0, int pageSize = 20,bool? homeShow = null)
        {
            try
            {
                TablePageDto<IDictionary<string, object>> tablePageDto = new TablePageDto<IDictionary<string, object>>();
                RefAsync<int> toalNumber = 0;
                var entity = await _productService.GetAllProductAsync(categorieId,toalNumber,true, homeShow, pageIndex, pageSize);
                var dto = _mapper.MapList<ProductWebListDTO>(entity);
                var lang = _langSplitHelper.ConvertToLang<ProductWebListDTO>(dto,language);
                tablePageDto.Data = lang;
                tablePageDto.PageIndex = pageIndex;
                tablePageDto.PageSize = pageSize;
                tablePageDto.ToalNumber = toalNumber;
                return new Success(tablePageDto);
            }   
            catch (Exception ex)
            {
                return new Error(ex);
            }
        }

        /// <summary>
        /// 获取单个商品
        /// </summary>
        /// <param name="language"></param>
        /// <param name="id"></param>
        /// <returns></returns>
        [HttpGet]
        public async Task<ActionResult> GetProductSingle(LangType language, int id)
        {
            try
            {
                var entity = await _productService.GetSingleProductByIdAsync(id);
                var currencies = await _productPriceCurrenciesService.GetProductPriceCurrenciesById(entity.Currencies);

                if (entity == null)
                {
                    throw new Exception("商品不存在");
                }
                var dto = _mapper.Map<ProductWebSingleDTO>(entity);
                var lang = _langSplitHelper.ConvertToLang(dto, language);
                lang["Currencies"] = currencies.Unit;
                return new Success(lang);
            }
            catch (Exception ex)
            {
                return new Error(ex);
            }
        }
    }
}
